﻿using Caliburn.Micro;
using FMOD;
using SmallTune.Messages;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace SmallTune.ViewModels
{
    [Export(typeof(PlayerViewModel))]
    public class PlayerViewModel : Conductor<object>.Collection.OneActive, IHandle<PlaySongMessage>
    {
        private Channel fChannel;
        private Sound fSound;
        private FMOD.System fSystem;
        private FMOD.RESULT FMOD_RESULT = FMOD.RESULT.OK;

        private string _displayTotalTime;

        public string displayTotalTime
        {
            get
            {
                return _displayTotalTime;
            }
            set
            {
                _displayTotalTime = value;
                NotifyOfPropertyChange(() => displayTotalTime);
            }
        }

        [ImportingConstructor]
        public PlayerViewModel(IEventAggregator eventAggregator)
        {
            PlaylistModel = new PlaylistViewModel(eventAggregator);
            DisplayName = "SmallTune";
            //Get the event aggregator
            _eventAggregator = eventAggregator;
            _eventAggregator.Subscribe(this);
            ActivateItem(PlaylistModel);

        }

        PlaylistViewModel PlaylistModel;
        private IEventAggregator _eventAggregator;

        public void PlaySong(string song)
        {
            FMOD_RESULT = FMOD.Factory.System_Create(ref this.fSystem);
            FMOD_RESULT = fSystem.init(1, FMOD.INITFLAGS.NORMAL, (IntPtr)null);

            //Read tags first
            //TagLib.File tagfile = TagLib.File.Create(song);

            //string title = tagfile.Tag.Title;

            //var song = new Song();
            //var album = new Album { name = tagfile.Tag.Album };
            //var artist = new Artist { name = tagfile.Tag.FirstAlbumArtist };

            //song.title = tagfile.Tag.Title;
            //song.durationInSeconds = 0;

            //tagfile.Dispose();



            fSystem.createSound(song, (FMOD.MODE._2D | FMOD.MODE.HARDWARE | FMOD.MODE.ACCURATETIME | FMOD.MODE.CREATESTREAM), ref fSound);

            fSystem.playSound(FMOD.CHANNELINDEX.FREE, fSound, false, ref fChannel);

            uint seconds = 0;
            fSound.getLength(ref seconds, FMOD.TIMEUNIT.MS);
            TimeSpan t = TimeSpan.FromMilliseconds(seconds);

            //song.AddArtist(artist);
            //album.AddSong(song);
            //artist.AddAlbum(album);

            displayTotalTime = string.Format("{0:D2}:{1:D2}",
                                    t.Minutes,
                                    t.Seconds);
            //displayTitle = title;
            //displayAlbum = song.listOfAlbums[0].name;
            //displayArtist = song.listOfArtists[0].name;
        }

        public void Handle(PlaySongMessage message)
        {
            if (message != null)
            {
                PlaySong(message.path);
            }
        }
    }
}
